package com.example.SearchAmbitus.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.example.SearchAmbitus.R;
import model.CategoryManager;
import model.LocationManager;
import org.json.JSONException;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Bxh on 2014/11/26.
 */
public class CategoryActivity extends Activity {
    private ListView listView_category;
    private Button btn_category_goBack;
    private TextView textView_lv2Title;
    private List<Map<String, Object>> mData;
    private String q;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_category);

        listView_category = (ListView) findViewById(R.id.listView_category);
        btn_category_goBack = (Button) findViewById(R.id.btn_category_goBack);
        textView_lv2Title = (TextView) findViewById(R.id.textView_lv2Title);

        btn_category_goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        String name = (String) intent.getExtras().get("name");
        id = Integer.parseInt(intent.getExtras().get("id").toString());
        Log.d("id", id + "");


        //获取二级列表数据
        CategoryManager categoryManager = new CategoryManager();
        mData = categoryManager.getLv1CategoryData(id);
        Mylvadapter mylvadapter = new Mylvadapter();
        listView_category.setAdapter(mylvadapter);

        textView_lv2Title.setText(name);//子菜单标题



    }


    public class Mylvadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final View view;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.item_lv2, parent, false);
            } else {
                view = convertView;
            }
            TextView textView_item_lv2 = (TextView) view.findViewById(R.id.textView_item_lv2);
            textView_item_lv2.setText((String) mData.get(position).get("name"));
            textView_item_lv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String name = (String) mData.get(position).get("name");

                    LocationManager locationManager = new LocationManager();
                    double lat = locationManager.getLat();
                    double lon = locationManager.getLon();

                    Intent intent = new Intent(CategoryActivity.this,POIListActivity.class);
                    intent.putExtra("lat",lat);
                    intent.putExtra("lon",lon);
                    intent.putExtra("name",name);
                    startActivity(intent);


//                    Toast.makeText(CategoryActivity.this,name,Toast.LENGTH_SHORT).show();
                }
            });


            return view;
        }


    }


}
